package com.core.system.controller;

import com.core.system.service.ISystemTemplateService;
import com.pojo.req.template.InsertTemplateReq;
import com.pojo.req.template.UpdateTemplateReq;
import com.pojo.resp.template.TemplateDetailsResp;
import com.pojo.resp.template.TemplatePageResp;
import com.pojo.resp.template.TemplateParentResp;
import com.utility.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单管理
 * @author: hw
 * @date: 2020/12/28 10:56
 */
@RestController
@RequestMapping("/api/system/template")
public class TemplateController {

    @Autowired
    private ISystemTemplateService templateService;

    /**
     * 查询当前用户菜单
     * @return
     */
    @GetMapping("/select")
    public Result<List<TemplatePageResp>> selectTemplateList(){
        return Result.returnCodeSuccess(templateService.getTemplateList());
    }

    /**
     * 菜单节点
     * @param pid 父级id
     * @return
     */
    @PostMapping("/nodes")
    public Result<List<TemplateParentResp>> selectTemplateAllList(@RequestParam(value = "pid",required = false) String pid){
        List<TemplateParentResp> templateList = templateService.getTemplateAllList(pid);
        return Result.returnCodeSuccess(templateList);
    }

    /**
     * 新增菜单
     * @param insertTemplateReq
     * @return
     */
    @PostMapping("/insert")
    public Result insertTemplateList(@RequestBody @Validated InsertTemplateReq insertTemplateReq){
         templateService.insertTemplate(insertTemplateReq);
        return Result.returnCodeSuccess();
    }

    /**
     * 修改菜单
     * @param updateTemplateReq
     * @return
     */
    @PostMapping("/update")
    public Result insertTemplateList(@RequestBody @Validated UpdateTemplateReq updateTemplateReq){
        templateService.updateTemplate(updateTemplateReq);
        return Result.returnCodeSuccess();
    }

    @GetMapping("details")
    public Result<TemplateDetailsResp> details(@RequestParam("id")Long id){
        return Result.returnCodeSuccess(templateService.details(id));
    }



    @GetMapping("delete")
    public Result<Integer> delete(@RequestParam("id")Long id){
        templateService.deleteById(id);
        return Result.returnCodeSuccess();
    }
}
